package com.fastCms.business.bim.manage.impl;

import com.fastCms.business.bim.daos.DataProdMapper;
import com.fastCms.business.bim.daos.DeviceMapper;
import com.fastCms.business.bim.manage.ProdDeviceService;
import com.fastCms.business.bim.pojo.entity.DataProd;
import com.fastCms.business.bim.pojo.po.ProdChartPO;
import com.fastCms.business.bim.pojo.po.ProdPO;
import com.fastCms.business.bim.pojo.vo.CcqDeviceVO;
import com.fastCms.business.bim.pojo.vo.DataProdVO;
import com.fastCms.business.frame.pojo.PageParam;
import com.fastCms.business.frame.utils.opc.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import static com.fastCms.business.frame.constant.iotApi.IotApiConst.DCS_CHART;
import static java.time.LocalDateTime.now;

/**
 * @Author: RogersZheng
 * @Date: 2021/6/24 2:10 下午
 * @Description: com.fastCms.customer.work.manage.impl
 */
@Transactional(rollbackFor = Exception.class)
@Component
@Slf4j
public class ProdDeviceServiceImpl implements ProdDeviceService {

    @Autowired
    private DataProdMapper dataProdMapper;

    @Resource
    private DeviceMapper deviceMapper;


    @Override
    public PageInfo<DataProdVO> getData(PageParam<ProdPO> pageParam) {
        ProdPO queryFields = pageParam.getQueryFields();
        PageInfo<DataProdVO> result = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize())
            .doSelectPageInfo(() ->
                deviceMapper.getProdDevice(queryFields));
        return result;
    }


    public PageInfo<DataProdVO> getCCQProdDevice(PageParam<ProdPO> pageParam) {
        ProdPO queryFields = pageParam.getQueryFields();
        PageInfo<DataProdVO> result = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize())
            .doSelectPageInfo(() ->
                deviceMapper.getCCQProdDevice(queryFields));
        return result;
    }

    @Override
    public List<DataProd> getDeviceData(ProdChartPO param) {
        List<DataProd> deviceDataVOS = new ArrayList<>();

        if (StringUtils.isEmpty(param.getStartTime())) {
            param.setStartTime(DateUtil.getPassDayS(1));
        }

        if (StringUtils.isEmpty(param.getEndTime())) {
            param.setEndTime(DateUtil.getCurrentDayE());
        }
        if (!param.getEndTime().contains(":")) {
            param.setEndTime(param.getEndTime() + " 23:59:59");
        }

        if (StringUtils.isNotEmpty(param.getDeviceCode())) {
            deviceDataVOS = dataProdMapper.showDeviceList2(param);
        }
        if (StringUtils.isNotEmpty(param.getDeviceId())) {
            deviceDataVOS = dataProdMapper.showDeviceList(param);
        }


        for (DataProd device : deviceDataVOS) {
            device.setLineName(DCS_CHART.get(param.getDeviceCode()));
        }

        if (deviceDataVOS == null || deviceDataVOS.isEmpty()) {
            deviceDataVOS = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                DataProd dataProd = new DataProd();
                dataProd.setStatus(-1);
                dataProd.setRemark("0");
                dataProd.setCreateTime(now());
                dataProd.setLineName(DCS_CHART.get(param.getDeviceCode()));
                deviceDataVOS.add(dataProd);
            }
        }
//        while (deviceDataVOS.size() > 8) {
//            Iterator<DataProd> iter = deviceDataVOS.iterator();
//            while (iter.hasNext()) {
//                DataProd item = iter.next();
//                int random = new java.util.Random().nextBoolean() ? 1 : 0;
//                if (random == 1 && (item.getStatus() == 0 || item.getStatus() == -1)) {
//                    iter.remove();
//                }
//            }
//        }
        return deviceDataVOS;
    }

    @Override
    public PageInfo<CcqDeviceVO> getCcqData(PageParam<ProdPO> pageParam) {

        PageInfo<DataProdVO> deviceInfoVOPageInfo = getCCQProdDevice(pageParam);
        List<CcqDeviceVO> ccqDeviceVOS = new ArrayList<>();
        List<DataProdVO> deviceInfoVOS = deviceInfoVOPageInfo.getList();
        for (DataProdVO deviceInfoVO : deviceInfoVOS) {
            CcqDeviceVO ccqDeviceVO = new CcqDeviceVO();
            try {
                BeanUtils.copyProperties(ccqDeviceVO, deviceInfoVO);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            ccqDeviceVO.setCcqDl("0");
            ccqDeviceVO.setCcqYc("0");
            ccqDeviceVO.setCcqPl("0");
            ccqDeviceVO.setArea(deviceInfoVO.getArea());
            if (deviceInfoVO.getName().contains("DL")) {
                ProdChartPO prodChartPO = new ProdChartPO();
                prodChartPO.setDeviceId(deviceInfoVO.getName());
                DataProd dataProd = dataProdMapper.showDeviceDataOne(prodChartPO);
                if (dataProd == null) {
                    ccqDeviceVO.setCcqDl("0");
                } else {
                    ccqDeviceVO.setCcqDl(dataProd.getRemark());
                }

            }
            if (deviceInfoVO.getName().contains("YC")) {
                ProdChartPO prodChartPO = new ProdChartPO();
                prodChartPO.setDeviceId(deviceInfoVO.getName());
                DataProd dataProd = dataProdMapper.showDeviceDataOne(prodChartPO);
                if (dataProd == null) {
                    ccqDeviceVO.setCcqYc("0");
                } else {
                    ccqDeviceVO.setCcqYc(dataProd.getRemark());
                }
            }
            if (deviceInfoVO.getName().contains("PL")) {
                ProdChartPO prodChartPO = new ProdChartPO();
                prodChartPO.setDeviceId(deviceInfoVO.getName());
                DataProd dataProd = dataProdMapper.showDeviceDataOne(prodChartPO);
                if (dataProd == null) {
                    ccqDeviceVO.setCcqPl("0");
                } else {
                    ccqDeviceVO.setCcqPl(dataProd.getRemark());
                }
            }

            if (deviceInfoVO.getName().contains("灰窑")) {
                ccqDeviceVO.setCcqType("石灰窑");
            } else if (deviceInfoVO.getName().contains("高炉")) {
                ccqDeviceVO.setCcqType("高炉");
            } else if (deviceInfoVO.getName().contains("烧结")) {
                ccqDeviceVO.setCcqType("烧结");
            } else {
                ccqDeviceVO.setCcqType("烧结");
            }

            ccqDeviceVOS.add(ccqDeviceVO);
        }

        PageInfo<CcqDeviceVO> deviceVOPageInfo = new PageInfo<>();

        deviceVOPageInfo.setList(ccqDeviceVOS);
        deviceVOPageInfo.setPageNum(deviceInfoVOPageInfo.getPageNum());
        deviceVOPageInfo.setNextPage(deviceInfoVOPageInfo.getNextPage());
        deviceVOPageInfo.setTotal(deviceInfoVOPageInfo.getTotal());
        deviceVOPageInfo.setPages(deviceInfoVOPageInfo.getPages());
        deviceVOPageInfo.setPrePage(deviceInfoVOPageInfo.getPrePage());
        return deviceVOPageInfo;
    }


}
